package com.ruoyi.web.controller.reseller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.reseller.VsDramasResellerLog;
import com.ruoyi.system.service.reseller.IVsDramasResellerLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.reseller.VsDramasResellerBind;
import com.ruoyi.system.service.reseller.IVsDramasResellerBindService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * This file is part of 南阳迈特网络科技有限公司.
 * @company  南阳迈特网络科技有限公司
 * @link     https://www.nymaite.com
 * @document https://product.nymaite.cn
 * @contact  service@nymaite.com
 * @author nymaite
 */
/**
 * 用户分销信息Controller
 * 
 * @author zzx
 * @date 2024-04-30
 */
@Api(tags = "用户分销信息")
@RestController
@RequestMapping("/reseller/bind")
public class VsDramasResellerBindController extends BaseController
{
    @Autowired
    private IVsDramasResellerBindService vsDramasResellerBindService;

    @Autowired
    private IVsDramasResellerLogService vsDramasResellerLogService;

    /**
     * 查询用户分销信息列表
     */
    @GetMapping("/list")
    @ApiOperation("查询用户分销信息列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "微博", required=true, dataType = "int"),
            @ApiImplicitParam(name = "username", value = "用户名", required=true)
    })
    public TableDataInfo list(VsDramasResellerBind vsDramasResellerBind)
    {
        startPage();
        List<VsDramasResellerBind> list = vsDramasResellerBindService.selectVsDramasResellerBindList(vsDramasResellerBind);
        return getDataTable(list);
    }

    /**
     * 导出用户分销信息列表
     */
//    @ApiOperation("导出用户分销信息列表")
    @Log(title = "用户分销信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VsDramasResellerBind vsDramasResellerBind)
    {
        List<VsDramasResellerBind> list = vsDramasResellerBindService.selectVsDramasResellerBindList(vsDramasResellerBind);
        ExcelUtil<VsDramasResellerBind> util = new ExcelUtil<VsDramasResellerBind>(VsDramasResellerBind.class);
        util.exportExcel(response, list, "用户分销信息数据");
    }

    /**
     * 获取用户分销信息详细信息
     */
//    @ApiOperation("获取用户分销信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(vsDramasResellerBindService.selectVsDramasResellerBindById(id));
    }

    /**
     * 新增用户分销信息
     */
//    @ApiOperation("新增用户分销信息")
    @Log(title = "用户分销信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VsDramasResellerBind vsDramasResellerBind)
    {
        return toAjax(vsDramasResellerBindService.insertVsDramasResellerBind(vsDramasResellerBind));
    }

    /**
     * 修改用户分销信息
     */
//    @ApiOperation("修改用户分销信息")
    @Log(title = "用户分销信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VsDramasResellerBind vsDramasResellerBind)
    {
        return toAjax(vsDramasResellerBindService.updateVsDramasResellerBind(vsDramasResellerBind));
    }

    /**
     * 删除用户分销信息
     */
//    @ApiOperation("删除用户分销信息")
    @Log(title = "用户分销信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vsDramasResellerBindService.deleteVsDramasResellerBindByIds(ids));
    }

    /**
     * 获取用户分销信息详细信息
     */
//    @ApiOperation("获取用户分销信息详细信息")
    @PostMapping(value = "/getInfo")
    public AjaxResult getResellerInfo(@RequestBody VsDramasResellerBind vsDramasResellerBind)
    {
        if (getLoginUser().getVsUser() == null) {
            return error("暂未登录！");
        }
        vsDramasResellerBind.setUserId(getLoginUser().getVsUser().getId());
        List<VsDramasResellerBind> bindList = vsDramasResellerBindService.selectVsDramasResellerBindList(vsDramasResellerBind);
        if (bindList.size() == 0) {
            return success(new VsDramasResellerBind());
        }
        VsDramasResellerBind resellerBind = bindList.get(0);
        JSONObject jsonObject = JSONObject.parseObject(resellerBind.getResellerJson());
        resellerBind.setResellerJsonObject(jsonObject);
        resellerBind.setUser(getLoginUser().getVsUser());
        VsDramasResellerLog resellerLog = new VsDramasResellerLog();
        resellerLog.setResellerUserId(Math.toIntExact(getLoginUser().getVsUser().getId()));
        List<VsDramasResellerLog> logs = vsDramasResellerLogService.selectVsDramasResellerLogList(resellerLog);
        BigDecimal money = new BigDecimal(0);
        for (VsDramasResellerLog log : logs) {
            money.add(BigDecimal.valueOf(log.getTotalMoney()));
        }
        resellerBind.setResellerMoney(money);
        return success(resellerBind);
    }

}
